﻿using Microsoft.AspNetCore.SignalR;

namespace NETCORE.TSignalR
{
    public class CustomHub : Hub
    {
        private readonly ILogger<CustomHub> _logger;

        public CustomHub(ILogger<CustomHub> logger)
        {
            _logger = logger;
        }

        public override Task OnConnectedAsync()
        {
            return base.OnConnectedAsync();
        }

        public override Task OnDisconnectedAsync(Exception? exception)
        {
            return base.OnDisconnectedAsync(exception);
        }


        public Task SendMessageCaller( string msg)
        {
            _logger.LogInformation("接受数据{0}", msg);
            return Clients.Caller.SendAsync("ReceiveCaller", $"自已收到消息，回复信息:{msg}");
        }

        public Task SendMessage(string id, string msg)
        {
            _logger.LogInformation("接受数据{0},{1}", id, msg);
            return Clients.All.SendAsync("ReceiveMessage", $"全体人员注意,{msg}，发送者：{id}");
        }


        public Task SendMessageOther(string id, string msg)
        {
            _logger.LogInformation("接受数据{0},{1}", id, msg);
            return Clients.Others.SendAsync("ReceiveMessage", $"其他人员注意,{msg}，发送者：{id}");
        }
    }     
}
